package test;

public class LongestCommonSubstring {

    public static int longestCommonSubstring(String str1, String str2) {
        int m = str1.length();
        int n = str2.length();

        // 创建一个二维数组 dp，初始值为 0
        int[][] dp = new int[m + 1][n + 1];
        
        // 记录最长公共子串的长度
        int maxLength = 0;

        // 遍历两个字符串
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                    maxLength = Math.max(maxLength, dp[i][j]);
                } else {
                    dp[i][j] = 0;
                }
            }
        }
        
        return maxLength;
    }

    public static void main(String[] args) {
        // 测试数据
        String str1 = "00000000001101011001000101000001";
        String str2 = "01001010010101100100010100000100";

        // 计算并输出最长公共子串的长度
        System.out.println(longestCommonSubstring(str1, str2));
    }
}
